using System.Collections;
using System.Collections.Generic;
using Framework.Library.Core.Config;
using Framework.Library.Core.Data.DataEngine;
using Framework.Library.Data.DAL;
using Framework.Library.Data.IDAO;
using Framework.Library.Core.Utils.Database;
using System.Data;
using System.Text;
using System;

namespace Framework.Library.Data.DAO.Oracle
{
    [Serializable]
    internal class TeenioPaymentManager : ITeenioPaymentManager
    {
        public int CheckPayment(int userId, int audioId)
        {
            QueryBuilder builder = new QueryBuilder("TEENIO_PAYMENTLOG");
            StringBuilder sb = new StringBuilder();
            sb.Append("DECLARE @RETURNVALUE int");
            sb.Append(" SET @RETURNVALUE = (SELECT top 1 ID from NS_TEENIO_PAYMENTLOG where PaygateID = @USERID AND ItemType = 2 AND DATEDIFF(DAY, convert(datetime, substring(Convert(varchar, Item), 1, 8), 111), GETDATE()) < 30 order by ID desc)");
            sb.Append(" if(ISNULL(@RETURNVALUE, 0) = 0)");
            sb.Append(" BEGIN");
            sb.Append(" SET @RETURNVALUE =  (select ID from NS_TEENIO_PAYMENTLOG where PaygateID = @USERID AND ItemType = 1 AND Item = @AUDIO)");
            sb.Append(" END");
            sb.Append(" SELECT ISNULL(@RETURNVALUE, 0)");

            var parameters = new ArrayList();
            parameters.Add(new object[] { "USERID", userId });
            parameters.Add(new object[] { "AUDIO", audioId });

            builder.Query = sb.ToString();
            builder.Parameters = parameters;
            builder.QueryType = QueryTypes.TSql;
            return (int)GeneralConnection.ExecuteScalar(CommandType.Text, builder);
        }
    }
}